package com.canbot.u05.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;

public class BluetoothUtil {

        private static String TAG = "BluetoothUtil";

        private static BluetoothSocket btSocket;

        static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";

        /**
         * 初始化蓝牙模块
         *
         * @param context
         * @return
         */
        public static BluetoothAdapter initBluetooth(Context context) {

                Logger.i(TAG, "initBluetooth ");
                // 检查设备是否支持蓝牙
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                if (adapter == null) {
                        Logger.i(TAG, "设备不支持蓝牙！  ");
                }
                // 打开蓝牙
                if (!adapter.isEnabled()) {
                        Logger.i(TAG, "打开蓝牙！    ");
                        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        // 设置蓝牙可见性，最多300秒
                        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                        context.startActivity(intent);
                }
                return adapter;

        }

        /**
         * 配对设备
         *
         * @param context
         * @param deviceName
         */
        public static void pairBluetoothDevice(Context context,
                                               BluetoothDevice device) {

                BluetoothAdapter adapter = initBluetooth(context);

                // 搜索蓝牙设备的过程占用资源比较多，一旦找到需要连接的设备后需要及时关闭搜索
                adapter.cancelDiscovery();
                // 获取蓝牙设备的连接状态
                Method createBondMethod;
                try {
                        createBondMethod = BluetoothDevice.class.getMethod("createBond");
                        createBondMethod.invoke(device);
                }
                catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

        /**
         * 连接设备
         *
         * @param context
         * @param deviceName
         */
        public static void connectBluetoothDevice(Context context,
                                                  BluetoothDevice device) {
                BluetoothAdapter adapter = initBluetooth(context);
                // 搜索蓝牙设备的过程占用资源比较多，一旦找到需要连接的设备后需要及时关闭搜索
                adapter.cancelDiscovery();
                UUID uuid = UUID.fromString(SPP_UUID);
                try {
                        btSocket = device.createRfcommSocketToServiceRecord(uuid);
                        Logger.d("BlueToothTestActivity", "开始连接...");
                        btSocket.connect();
                }
                catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

}
